package com.tanhua.server.controller;

import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.dubbo.api.UserApi;

import com.tanhua.server.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /*通过在控制器方法中返回ResponseEntity对象，
    可以非常方便的设置http主体内容、响应状态码以及头信息。*/
    //通过手机号查询用户
    @GetMapping("/findByMobile")
    public ResponseEntity<Object> findByMobile(String mobile) {
        return userService.findByMobile(mobile);
    }

    //新增用户
    @PostMapping
    public ResponseEntity<Object> save(@RequestBody User user) {
        return userService.save(user);
    }

    //手机号短信发送
    @PostMapping("/login")
    public ResponseEntity<Object> login(@RequestBody Map<String, String> params) {
        return userService.SendAuthCode(params.get("phone"));
    }

    //验证码校验
    @PostMapping("/loginVerification")
    public ResponseEntity<Object> loginVerification(@RequestBody Map<String, String> params) {
        return userService.loginVerification(params);
    }

    //第一次登陆填写用户信息
    @PostMapping("/loginReginfo")
    public ResponseEntity<Object> loginReginfo(@RequestBody UserInfo userInfo, @RequestHeader("Authorization") String token){
        return userService.saveUserInfo(userInfo,token);
    }

    //获取头像
    @PostMapping("/loginReginfo/head")
    public ResponseEntity<Object> head(@RequestHeader("Authorization") String token, @RequestBody MultipartFile headPhoto) throws IOException {
        return userService.updateUserHead(headPhoto,token);
    }
}
